package com.geektime.prepare_week1_servletWeb.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * 描述
 *
 * @author xgq
 * @version 1.0
 * @date 2021/06/08 22:46:38
 */
@WebServlet(name = "forwardServlet", value = "/ForwardServlet")
public class ForwardServlet extends HttpServlet {

    public ForwardServlet(){
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //request.setCharacterEncoding("gb2312");
        String destination = request.getParameter("destination");

        //跳转到/WEB-INF/web.xml
        if("file".equals(destination)){
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/web.xml");
            requestDispatcher.forward(request, response);
        }else if("jsp".equals(destination)){	//跳转到 /forward.jsp
            request.setAttribute("date", new Date());
            //传递一个Date对象给JSP页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/forward.jsp");
            dispatcher.forward(request, response);
        }else if("servlet".equals(destination)){	//跳转到另一个servlet
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/FirstServlet");
            requestDispatcher.forward(request, response);

        }else {
            //response.setCharacterEncoding("gb2312");
            response.getWriter().println("缺少参数。用法：" + request.getRequestURL() + "?destination=jsp或者file或者servlet");
        }
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.setCharacterEncoding("gb2312");
        doGet(request, response);
    }


}
